package com.wizz.trade.excel;

import com.alibaba.excel.EasyExcel;
import com.wizz.trade.information.entity.Svar;
import com.wizz.trade.information.service.SvarService;
import com.wizz.trade.template.service.PCDetailsService;
import com.wizz.trade.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 表格模块/测试
 *
 */
@RestController
@Slf4j
@RequestMapping("/excel")
public class ExcelController {

    @Resource
    private SvarService svarService;


    @GetMapping
    public void exportExcel(HttpServletResponse response){
        String filename = "test.xlsx";
        List<BuyConfirmTemplate> templateList = new ArrayList<>();
        for(int i = 1;i<=20;i++){
            BuyConfirmTemplate template = new BuyConfirmTemplate();
            template.setGrade("规格型号" + i);
            template.setManufacturers("生产厂家" + i);
            template.setTrademark("商标" + i);
            template.setVariety("品名" + i);
            template.setQuantity(Double.valueOf(100));
            template.setRealQuan(Double.valueOf(100));
            template.setUnitPrice(Double.valueOf(120));
            templateList.add(template);
        }
        ExcelBuildParams params = new ExcelBuildParams();
        params.setFileName(filename);
        params.setData(templateList);
        params.setForceNewRow(Boolean.TRUE);
       // params.setTemplatePath("src\\main\\resources\\采购-合同模板.xlsx");
        ExcelUtil.exportExcelWithLocalTemplate(response, params);
    }

    @PostMapping("/param2")
    public void exportExcel2(HttpServletResponse response, @RequestParam("file") MultipartFile file){
        String filename = "test.xlsx";
        List<BuyConfirmTemplate> templateList = new ArrayList<>();
        for(int i = 1;i<=20;i++){
            BuyConfirmTemplate template = new BuyConfirmTemplate();
            template.setGrade("规格型号" + i);
            template.setManufacturers("生产厂家" + i);
            template.setTrademark("商标" + i);
            template.setVariety("品名" + i);
            template.setQuantity(Double.valueOf(100));
            template.setRealQuan(Double.valueOf(100));
            template.setUnitPrice(Double.valueOf(120));
            templateList.add(template);
        }
        ExcelBuildParams params = new ExcelBuildParams();
        params.setFileName(filename);
        params.setData(templateList);
        ExcelUtil.exportExcelWithTemplateByUpload(file, response, params);
    }

    @GetMapping("read")
    public void readExcel(){
        String fileName = "src\\main\\resources\\工作簿1.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(fileName, Svar.class, new SvarListener(svarService)).sheet().doRead();
    }

}
